﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEditor;
using UnityEditor.MemoryProfiler;
using UnityEngine;

namespace PerfAssist.MemoryProfilerAdvanced.Editor.PAContrib.TrackerModes
{
    public class TrackerMode_File : TrackerMode_Base
    {
        protected override void Do_GUI()
        {
            GUILayout.Space(DrawIndicesGrid(0, 20));
        }

        public void LoadFile()
        {
            var pathName = EditorUtility.OpenFilePanel("Load Snapshot File", MemUtil.SnapshotsDir, "");
            if (string.IsNullOrEmpty(pathName))
                return;

            object packed = null;
            try
            {
                var bf = new BinaryFormatter();
                var fileName = pathName;
                if (fileName.EndsWith(".memsnap"))
                    using (Stream stream = File.Open(fileName, FileMode.Open))
                    {
                        packed = bf.Deserialize(stream);
                    }
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("load snapshot error ! msg ={0}", ex.Message));
                return;
            }

            if (packed != null)
            {
                Clear();

                var memInfo = new MemSnapshotInfo();
                if (memInfo.AcceptSnapshot(packed as PackedMemorySnapshot))
                    _snapshots.Add(memInfo);

                RefreshIndices();
                _selected = _snapshots.Count - 1;
            }
        }

        public void LoadSession()
        {
            var pathName = EditorUtility.OpenFolderPanel("Load Snapshot Folder", MemUtil.SnapshotsDir, "");
            if (string.IsNullOrEmpty(pathName))
                return;

            var packeds = new List<object>();
            try
            {
                var TheFolder = new DirectoryInfo(pathName);
                if (!TheFolder.Exists)
                    throw new Exception(string.Format("bad path: {0}", TheFolder));

                var bf = new BinaryFormatter();
                foreach (var file in TheFolder.GetFiles())
                {
                    var fileName = file.FullName;
                    if (fileName.EndsWith(".memsnap"))
                        using (Stream stream = File.Open(fileName, FileMode.Open))
                        {
                            packeds.Add(bf.Deserialize(stream));
                        }
                }

                if (packeds.Count == 0)
                {
                    MemUtil.NotifyError("no snapshots found.");
                    return;
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("load snapshot error ! msg ={0}", ex.Message));
                return;
            }

            Clear();

            foreach (var obj in packeds)
            {
                var memInfo = new MemSnapshotInfo();
                if (memInfo.AcceptSnapshot(obj as PackedMemorySnapshot))
                    _snapshots.Add(memInfo);
            }

            if (_snapshots.Count == 0)
            {
                MemUtil.NotifyError("empty snapshot list, ignored.");
                return;
            }

            RefreshIndices();
            _selected = _snapshots.Count - 1;
        }
    }
}